
#include <boost/function.hpp>
#include <boost/thread.hpp>
#include <boost/timer.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>

using boost::thread;

class CxTime 
{
	typedef boost::mutex				_Mutex;
	typedef boost::posix_time::ptime	_Time;
	
public:
	CxTime():m_thread(boost::bind(&CxTime::_timerThread,this)),b_spin(true)
	{
		time_t  t;
		time(&t);
		m_time=boost::posix_time::from_time_t(t);
	}
	~CxTime()
	{
		exit();
	}
public:
	boost::posix_time::ptime	getCurrent()
	{
		_Mutex::scoped_lock lock(m_mu);
		return m_time;
	}
	void		set(boost::posix_time::ptime t)
	{
		_Mutex::scoped_lock lock(m_mu);
		m_time=t;
	}
	void		exit()
	{
		b_spin=false;
		m_thread.interrupt();
		m_thread.join();
	}
protected:
	void		_timerThread()
	{
		while(b_spin)
		{
			boost::this_thread::sleep(boost::posix_time::seconds(1));
			_Mutex::scoped_lock lock(m_mu);
			m_time+=boost::posix_time::seconds(1);
			boost::this_thread::interruption_point();
		}
	}

	volatile	bool		b_spin;
	_Time					m_time;
	thread					m_thread;
	_Mutex					m_mu;
};






//
//
//class CxTimer
//{
//	typedef boost::function<void()>				_TimerFunc;
//	typedef boost::posix_time::time_duration	_TimeLong;
//
//public:
//	CxTimer(_TimeLong  t, _TimerFunc f):m_thread(boost::bind(&CxTimer::__TimerFunc,this)),
//									m_timedura(t),m_func(f)
//	{
//	}
//
//	void		stop()
//	{
//		b_spin=false;
//		m_thread.interrupt();
//		m_thread.join();
//	}
//protected:
//	void				__TimerFunc()
//	{
//		while(b_spin)
//		{
//		}
//	}
//private:
//
//	volatile	bool		b_spin;
//	_TimeLong				m_timedura;
//	thread					m_thread;
//	_TimerFunc				m_func;
//
//};
